/**
 * 
 */
package com.sad.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sad.util.G;

/**
 * @author zhaoqy
 *
 */
@SuppressWarnings({ "rawtypes" })
public final class RecordManager {

	private static Logger logger = LoggerFactory.getLogger(RecordManager.class);

	private static final Map<String, IRecord> procedures = new HashMap<String, IRecord>();

	public static IRecord loadRecord(String name) {

		// String procedureClassName = "com.sad.dao." + G.database.dialect + "." + name;
		String procedureClassName = G.system.record_package + "." + StringUtils.capitalize(name)
				+ "Record";

		if (procedures.get(procedureClassName) != null) {

			if (logger.isDebugEnabled()) {
				logger.debug("load [" + procedureClassName + "] from cache");
			}

			return procedures.get(procedureClassName);
		}

		try {
			IRecord procedure = (IRecord) Class.forName(procedureClassName).newInstance();
			procedures.put(procedureClassName, procedure);

			if (logger.isDebugEnabled()) {
				logger.debug("load [" + procedureClassName + "] with classloader");
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return procedures.get(procedureClassName);
	}
}
